<?php

namespace Biz\model\activity;

use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\ImgUrl as tImgUrl;

class ActivityPageTempl extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'activity_page_templ';

    //商品类型
    const ACTIVITY_PAGE_TEMPL_TYPE_ONE = 1;//一行一个
    const ACTIVITY_PAGE_TEMPL_TYPE_TWO = 2;//一行两个

    //状态
    const STATUS_NORMAL = 1;//正常
    const STATUS_DELETE = 0;//删除

    public static function create($data)
    {
        $rows = self::insert($data)->rowCount();
        if ($rows) {
            return $rows;
        }
        return false;
    }

    public static function getListByActivityId($activityId, $order = ['sort' => 'ASC'])
    {
        $list = self::select('*', ['activity_id' => $activityId, 'status' => self::STATUS_NORMAL, 'ORDER' => $order]);
        foreach ($list as &$info) {
            self::format($info);
        }
        return $list;
    }

    public static function delListByActivityId($activityId)
    {
        $ret = self::update(['status' => self::STATUS_DELETE], ['activity_id' => $activityId]);
        if ($ret->rowCount()) {
            return true;
        }
        return false;
    }

    public static function delByTemplId($templId)
    {
        $ret = self::update(['status' => self::STATUS_DELETE], ['id' => $templId]);
        if ($ret->rowCount()) {
            return true;
        }
        return false;
    }

    private static function format(&$info)
    {
        foreach ($info as $key => $value) {
            switch ($key) {
                case 'banner':
                    if (!empty($value)) {
                        $info['banner_url'] = tImgUrl::getUrl(tImgUrl::APPTYPE_BIZ, $value, tImgUrl::IMAGE_SIZE_A_PIC);
                    }
                    break;
            }
        }
    }

}